﻿using DLL.NET.Login.Account.Entity.DTO;
using DLL.NET70.Infrastructure;
using DLL.NET70.Infrastructure.EventAggregator;
using DLL.NET70.Infrastructure.Prism.MVVM;
using DLL.Standard.Infrastructure.Entity;
using Prism.Commands;
using Prism.Events;
using Prism.Ioc;
using Prism.Mvvm;
using Prism.Regions;

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Windows.Media;

namespace Module.MDViews.ViewModels
{
    public class Drawer_NavigateBtnViewModel : CMVVMBase
    {
        private ObservableCollection<CNavigateButton> _navigateButtons = new ObservableCollection<CNavigateButton>();

        public ObservableCollection<CNavigateButton> NavigateButtons
        {
            get { return _navigateButtons; }
            set { SetProperty(ref _navigateButtons, value); }
        }

        private ObservableCollection<CNavigateButton> _allNavigateButtons = new ObservableCollection<CNavigateButton>();

        public ObservableCollection<CNavigateButton> allNavigateButtons
        {
            get { return _allNavigateButtons; }
            set { SetProperty(ref _allNavigateButtons, value); }
        }

        //EnableBtn
        private bool _enableBtn = true;

        public bool EnableBtn
        {
            get { return _enableBtn; }
            set { SetProperty(ref _enableBtn, value); }
        }

        /// <summary>
        /// 用于导航
        /// </summary>
        public DelegateCommand<CNavigateButton> NavigateCommand { get; set; }

        private IRegionManager _regionManager { get; set; }
        private IEventAggregator _eventAggregator;
        private adi_accountDTO _account;
        private IContainerExtension _container;

        public Drawer_NavigateBtnViewModel(IContainerExtension container,
            IRegionManager regionManager,
            IEventAggregator eventAggregator) : base(regionManager, eventAggregator)
        {
            _regionManager = regionManager;
            _container = container;
            _eventAggregator = eventAggregator;
            _account = container.Resolve<adi_accountDTO>();
            InitilizeAllBotton();
            NavigateButtons = new ObservableCollection<CNavigateButton>(allNavigateButtons);
            NavigateCommand = new DelegateCommand<CNavigateButton>(Navigate);
        }

        public override void Loaded()
        {
            //注册消息接收
            if (_eventAggregator != null)
            {
                _eventAggregator.GetEvent<CMessageEvent>().Subscribe(MessageReceived);//注册消息接收相应函数
                _eventAggregator.GetEvent<CCommandMessageEvent>().Subscribe(CommandMessageReceived);//注册消息接收相应函数
            }
            _account = _container.Resolve<adi_accountDTO>();
            int authority = _account.MAuthority;
            List<CNavigateButton> buttons = GetButton(authority);
            NavigateButtons = new ObservableCollection<CNavigateButton>(buttons);
            Navigate("PUC_AutoRun");
            base.Loaded();
        }

        public override void UnLoaded()
        {
            //注册消息接收
            if (_eventAggregator != null)
            {
                _eventAggregator.GetEvent<CMessageEvent>().Unsubscribe(MessageReceived);//注册消息接收相应函数
                _eventAggregator.GetEvent<CCommandMessageEvent>().Unsubscribe(CommandMessageReceived);
            }
            base.UnLoaded();
        }

        public override void CommandMessageReceived(CCommandMessage message)
        {
            if (message.CommandStr == GlobalCommand.UpdateNavigateBtn)
            {
                _account = _container.Resolve<adi_accountDTO>();
                NavigateButtons = new ObservableCollection<CNavigateButton>(GetButton(_account.MAuthority));
            }
            else if (message.CommandStr == GlobalCommand.EnableNavigateBtn)
            {
                if (message.value == "1")
                {
                    EnableBtn = true;
                }
                else
                {
                    EnableBtn = false;
                }
            }
            base.CommandMessageReceived(message);
        }

        private void InitilizeAllBotton()
        {
            allNavigateButtons = new ObservableCollection<CNavigateButton>()
            {
                 new CNavigateButton(){
                    UserBackground=new SolidColorBrush((Color)ColorConverter.ConvertFromString("#ff9f4c")),
                    Name="配药",
                    url="PUC_AutoRun",
                    Authority=0,
                    index=0,
                    BackGround="Blue",
                },
                new CNavigateButton(){
                    UserBackground=new SolidColorBrush((Color)ColorConverter.ConvertFromString("#ff9f4c")),
                    Name="账户管理",
                    url="PUC_AccountShell",
                    Authority=1,
                    index=1,

                    BackGround="Blue",
                },
                new CNavigateButton(){
                     UserBackground=new SolidColorBrush((Color)ColorConverter.ConvertFromString("#ff9f4c")),
                     Name="单机文件",
                     url="PUC_WorkFile",
                     Authority=1,
                     index=2,
                     BackGround="Blue",
                },
                new CNavigateButton(){
                     UserBackground=new SolidColorBrush((Color)ColorConverter.ConvertFromString("#ff9f4c")),
                     Name="药品字典",
                     url="PUC_ShowDicDrugs",
                     Authority=1,
                     index=3,
                     BackGround="Blue",
                },

                new CNavigateButton(){
                     UserBackground=new SolidColorBrush((Color)ColorConverter.ConvertFromString("#ff9f4c")),
                     Name="设备参数",
                     url="PUC_MS_Param",
                     Authority=2,
                     index=4,
                     BackGround="Blue",
                },
                 new CNavigateButton(){
                     UserBackground=new SolidColorBrush((Color)ColorConverter.ConvertFromString("#ff9f4c")),
                     Name="运动参数",
                     url="PUC_MSDMMotionParamShell",
                     Authority=2,
                     index=5,
                     BackGround="Blue",
                },
                 new CNavigateButton(){
                     UserBackground=new SolidColorBrush((Color)ColorConverter.ConvertFromString("#ff9f4c")),
                     Name="串口设备",
                     url="PUC_MSComDeviceShell",
                     Authority=2,
                     index=6,
                     BackGround="Blue",
                },
                new CNavigateButton(){
                     UserBackground=new SolidColorBrush((Color)ColorConverter.ConvertFromString("#ff9f4c")),
                    Name="回原点",
                    url="PUC_MSDMHomeShell",
                    Authority=2,
                    index=7,
                    BackGround="Blue",
                },
                new CNavigateButton(){
                     UserBackground=new SolidColorBrush((Color)ColorConverter.ConvertFromString("#ff9f4c")),
                    Name="单轴调试",
                    url="PUC_AxisDebug",
                    Authority=2,
                    index=8,
                    BackGround="Blue",
                },
                new CNavigateButton(){
                     UserBackground=new SolidColorBrush((Color)ColorConverter.ConvertFromString("#ff9f4c")),
                     Name="IO监测",
                     url="PUC_IOMonitor",
                     Authority=2,
                     index=9,
                     BackGround="Blue",
                },

                //new CNavigateButton(){
                //     UserBackground=new SolidColorBrush((Color)ColorConverter.ConvertFromString("#ff9f4c")),
                //     Name="舵机测试",
                //     url="PUC_DJShell",
                //     Authority=2,
                //     index=9,
                //     BackGround="Blue",
                //},

                //new CNavigateButton(){
                //     UserBackground=new SolidColorBrush((Color)ColorConverter.ConvertFromString("#ff9f4c")),
                //     Name="切换主题",
                //     url="PaletteSelector",
                //     Authority=10,
                //     BackGround="Blue",
                //},
            };
        }

        private List<CNavigateButton> GetButton(int autority)
        {
            return allNavigateButtons.Where(w => w.Authority <= autority).OrderBy(od => od.index).ToList();
        }

        //PUC_DSZ_Param
        private void Navigate(string url)
        {
            _regionManager.RequestNavigate(CGlobalRegionNames.MainContentRegion, url);
        }

        /// <summary>
        /// 导航函数
        /// </summary>
        /// <param name="obj"></param>
        private void Navigate(CNavigateButton obj)
        {
            //加入判断，如果是空对象或者命名空间为空则跳出
            if (obj == null || string.IsNullOrEmpty(obj.url))
            {
                return;
            }
            //_regionManager.Regions["ContentRegion"].RequestNavigate(obj.url, back =>
            //{
            //    //使用回调函数，添加路由日志
            //    //journal = back.Context.NavigationService.Journal;
            //});
            Navigate(obj.url);
        }
    }
}